package svc

import (
	"cloud-entropy-storage-system/server/internal/types/form"
	"cloud-entropy-storage-system/server/model/dir"
	"go.uber.org/zap"
	"time"
)

type DirSvc struct {
	log     *zap.Logger
	operate *dir.Operate
}

func NewDirSvc(log *zap.Logger, operate *dir.Operate) *DirSvc {
	return &DirSvc{log: log, operate: operate}
}

func (d *DirSvc) List(page, limit int) (info []dir.FileDir, err error) {
	data, err := d.operate.List(page, limit)
	if err != nil {
		return nil, err
	}
	return data, nil
}

func (d *DirSvc) Count() int {
	num, err := d.operate.Count()
	if err != nil {
		d.log.Error("d.operate.Count", zap.Error(err))
		return 0
	}
	return num
}

func (d *DirSvc) Set(f form.Dir) error {
	if err := d.operate.Set(dir.FileDir{Name: f.Name, IsDel: 0, Level: 1,
		CreatedAt: time.Now().Format("2006-01-02 15:04:05")}); err != nil {
		d.log.Error("d.operate.Set", zap.Error(err))
		return err
	}
	return nil
}

func (d *DirSvc) Delete(id int) error {
	return d.operate.Delete(dir.FileDir{ID: id})
}
